
import java.awt.HeadlessException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

public class Client {
	public static void main(String[] args) throws IOException {

		Socket kkSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String commandString  = new String(); 
		String portString = "3000"; 
		String command ="";
		int port = 3000 ;

		try
		{
		System.setProperty("java.awt.headless", "true");
		}
		catch(HeadlessException he)
		{
			System.out.println("Headless mode not supported ");
			System.exit(0);
		}
		
		if(args.length==0)
		{
			System.out.println("Invalid Number of arguments");
			System.exit(-1);
		}
		else if (args.length==1)
		{
			command = args[0];
		}
		
		else if (args.length==2)
		{
			portString = args[0];
		
			try
			{
				port = Integer.parseInt(portString);
				
				if(port<1 || port>65535)
				{
					System.out.println("Invalid Port Number");
					System.exit(-1);
				}
				command = args[1];		
				
			
			}
			catch(NumberFormatException n)
			{
			System.out.println("Invalid Port number");
			System.exit(-1);
			}
			
		}
		
		if(!(command.equals("addbird") || command.equals("addsighting")|| command.equals("listbirds")|| command.equals("listsightings")
				|| command.equals("remove")||command.equals("quit")))
		{
			System.out.println("Invalid argument ");
			System.exit(-1);
		}
	
		if(command.equals("addbird"))
		{
			int heightflag = 0 ; 
			int weightflag = 0 ; 
			commandString+=command+",";       
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("Enter the name of the Bird");
			String name = stdIn.readLine() ; 
			commandString+=name+",";
			System.out.println("Enter the color of the Bird");
			String color =stdIn.readLine();
			commandString+=color+",";
			System.out.println("Enter the height of the Bird");
			String height = stdIn.readLine() ; 
			while(heightflag==0)
			{
				try{
					Integer heightInt = Integer.parseInt(height);

					if(heightInt<=0)
					{
						System.out.println("Invalid Height , enter a valid height");
						height = stdIn.readLine() ; 
					}

					else
					{
						heightflag = 1 ; 
						commandString+=heightInt+",";
					}
				}
				catch(NumberFormatException e){
					System.out.println("Invalid integer, enter a valid height");
					height = stdIn.readLine() ; 

				}


			}
			commandString+=height+",";
			
			System.out.println("Enter the weight of the Bird");
			String weight = stdIn.readLine() ; 

			while(weightflag==0)
			{
				try{
					Integer weightInt = Integer.parseInt(weight);

					if(weightInt<=0)
					{
						System.out.println("Invalid weight , enter a valid height");
						weight = stdIn.readLine() ; 
					}

					else
					{
						weightflag = 1 ; 
					}
				}
				catch(NumberFormatException e){
					System.out.println("Invalid integer, enter a valid weight");
					weight = stdIn.readLine() ; 
				}
			}

			commandString+=weight;

		}
		else if(command.equals("addsighting"))
		{
			int dateFlag = 0 ; 
			commandString+=command+",";

			System.out.println("Enter the bird Name");
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

			String name = stdIn.readLine();
			
			commandString+=name + ",";

			System.out.println("Enter the date and time in dd-MM-yyyy hh:mm format");
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm");
			String dateString = stdIn.readLine() ; 
			Date date = new Date();

			while(dateFlag==0)
			{
			try {
				date = sdf.parse(dateString);

				dateFlag = 1 ;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				System.out.println("Incorrect format \n " +
						"Enter the date and time in dd-MM-yyyy hh:mm format");
				dateString = stdIn.readLine();
				}

			}
			 
			commandString+=dateString+",";
			
			System.out.println("Enter the Location"); 
			String location = stdIn.readLine(); 
			
			commandString+=location;

		}

		else if (command.equals("listbirds"))
		{
			commandString+=command;
	//		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));				
		}

		else if(command.equals("listsightings"))
		{
			int dateFlag = 0 ; 
			commandString+=command+",";
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

			System.out.println("Enter the name of the bird or the regular expression");
			String name = stdIn.readLine();
			commandString+=name+",";			

	System.out.println("Enter the start date and time in dd-MM-yyyy hh:mm format");
			
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm");
			String startdateString = stdIn.readLine() ; 
			while(dateFlag==0)
			{
				try {
					sdf.parse(startdateString);
					dateFlag = 1 ; 
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("Incorrect format , please enter the date again");
				startdateString = stdIn.readLine();
				}
			}
			commandString+=startdateString+",";
		
			dateFlag = 0 ; 
		
			System.out.println("Enter the end date and time in dd-MM-yyyy hh:mm format");
			
			String enddateString = stdIn.readLine() ; 
			while(dateFlag==0)
			{
				try {
					sdf.parse(enddateString);
					dateFlag = 1 ; 
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("Incorrect format , please enter the date again");
				enddateString = stdIn.readLine();
				}
			}
			commandString+=enddateString;
		
		}
		else if(command.equals("remove"))
		{
			commandString+=command+"," ; 
			BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

			System.out.println("Enter the name of the bird");
			String name = stdIn.readLine();
			commandString+=name;	
		}
		else if(command.equals("quit"))
		{
			commandString+=command ; 

		}

		BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
		String fromServer;
		String fromUser;

	
		try {
			kkSocket = new Socket("localhost", port);
			out = new PrintWriter(kkSocket.getOutputStream(), true);
			out.println(commandString);
			in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("Could not connect to the Host : UnknownHostException");
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for the connection to the server");
			System.exit(1);
		}

		
		
		try{
			/***
			 * If you did not get a response from the server , WAIT !!!!!
			 */
			/***
			 * Else , show the response and get out of here !!!
			 */
			if ((fromServer = in.readLine()) != null) {
	
			
			 if(command.equals("listsightings"))
			{
				StringTokenizer tokenizer = new StringTokenizer(fromServer,",");
				
				if(tokenizer.countTokens()>3)
				while(tokenizer.hasMoreTokens())
				{
					System.out.println(tokenizer.nextToken()+"\t"+tokenizer.nextToken()+"\t"+tokenizer.nextToken()+"\n");
				}
				else
				{
					System.out.println("No element found");
				}
			}
			
			 else if(command.equals("listbirds"))
				{
					StringTokenizer tokenizer = new StringTokenizer(fromServer,",");
					
					if(tokenizer.countTokens()>4)
					while(tokenizer.hasMoreTokens())
					{
						System.out.println(tokenizer.nextToken()+"\t"+tokenizer.nextToken()+"\t"+tokenizer.nextToken()+"\t"+tokenizer.nextToken()+"\n");
					}
					else
					{
						System.out.println("No element found");
					}
				}
				 
			 else
			{
			System.out.println(fromServer);
			}	
			
		}	
		
		
			
		}
		catch(SocketException c)
		{
			System.out.println("Socket Connection Reset");
			System.exit(-1);
		}

		out.close();
		in.close();
		stdIn.close();
		kkSocket.close();
	}
}
